<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2023/6/14
 * Time: 16:13
 */
namespace app\index\controller;

use app\middleware\Check;
use think\facade\Db;

class Skill {

    protected $middleware = [
        Check::class => ['except' 	=> ['bannerList'] ]
    ];


    /**
     * 技巧章节列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function chapterList(){
        //获取技巧章节列表
        validateParam('city,skill_paper_id','city,skill_paper_id');
        //获取技巧试卷的所在城市
        $skill_paper = Db::name('skill_paper')
            ->where('id_',input('skill_paper_id'))
            ->find();
        //判断用户是否解锁了
        $info = Db::name('user_unlock_region')
            ->where('user_id',uid())
            ->where('region_name',$skill_paper['city_'])
            ->find();
        if(empty($info)){
            return error('未解锁');
        }
        //判断是否解锁了题库
        if($info['is_unlock_skill'] !== 1){
            return error('未解锁');
        }

        $list = Db::name('skill_chapter')
            ->where('skill_paper_id',input('skill_paper_id'))
            ->select();
        return success('技巧章节列表',$list);
    }


    /**
     * 技巧章节获取题目
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function questionByChapter(){
        validateParam('chapter_id','chapter_id');
        $chapter_id = input('chapter_id');
        //获取章节信息
        $chapter = Db::name('skill_chapter')
            ->where('id_',$chapter_id)
            ->find();
        //判断用户是否解锁了
        $info = Db::name('user_unlock_region')
            ->where('user_id',uid())
            ->where('region_name',$chapter['city_'])
            ->find();
        if(empty($info)){
            return error('未解锁');
        }
        //判断是否解锁了题库
        if($info['is_unlock_skill'] !== 1){
            return error('未解锁');
        }
        //获取章节的题目
        $list = Db::name('skill_chapter_question')
            ->alias('a')
            ->field('a.id_,a.skill_chapter_id,a.qid')
            ->join('question b','a.qid=b.id_')
            ->where('a.skill_chapter_id',$chapter_id)
            ->order('a.id_ asc')
            ->select();
        return success('章节获取题目',$list);
    }


    /**
     * 技巧试卷的所有技巧题库
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function questionByCity(){
        validateParam('skill_paper_id','skill_paper_id');
        $skill_paper_id = input('skill_paper_id');
        //获取技巧试卷的所在城市
        $skill_paper = Db::name('skill_paper')
            ->where('id_',input('skill_paper_id'))
            ->find();
        //判断用户是否解锁了
        $info = Db::name('user_unlock_region')
            ->where('user_id',uid())
            ->where('region_name',$skill_paper['city_'])
            ->find();
        if(empty($info)){
            return error('未解锁');
        }
        //判断是否解锁了题库
        if($info['is_unlock_skill'] !== 1){
            return error('未解锁');
        }
        $list = Db::name('skill_chapter_question')
            ->alias('a')
            ->field('a.id_,a.skill_chapter_id,a.qid')
            ->join('question b','a.qid=b.id_')
            ->where('a.skill_paper_id',$skill_paper_id)
            ->order('a.id_ asc')
            ->select();
        return success('技巧试卷的所有技巧题库',$list);
    }



    /**
     * 获取技巧题目详情
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function questionDetail(){
        validateParam('skill_id','skill_id');
        $skill = Db::name('skill_chapter_question')
            ->where('id_',input('skill_id'))
            ->find();
        //判断用户是否解锁了
        $info = Db::name('user_unlock_region')
            ->where('user_id',uid())
            ->where('region_name',$skill['city_'])
            ->find();
        if(empty($info)){
            return error('未解锁');
        }
        //判断是否解锁了题库
        if($info['is_unlock_skill'] !== 1){
            return error('未解锁');
        }
        $question = Db::name('question')
            ->where('id_',$skill['qid'])
            ->find();
        if(empty($question)){
            return error('题目不存在');
        }
        //获取用户是否收藏了该题目
        $collect = Db::name('user_collect')
            ->where('user_id',uid())
            ->where('question_id',$skill['qid'])
            ->find();
        if(empty($collect)){
            $question['is_collect'] = false;
        }else{
            $question['is_collect'] = true;
        }
        return success('题目详情',['question'=>$question,'skill'=>$skill]);
    }


    /**
     * banner列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function bannerList(){
        $list = Db::name('banner')
            ->where('is_deleted',0)
            ->select();
        return success('banner列表',$list);
    }
}